package com.iflyun.goodsshow.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.iflyun.goodsshow.entity.Analysis;
import com.iflyun.goodsshow.entity.DayDetail;
import com.iflyun.goodsshow.entity.Goods;
import com.iflyun.goodsshow.entity.Shop;
import com.iflyun.goodsshow.service.GoodsService;
import com.iflyun.goodsshow.utils.EasyuiDatagridJson;

import net.sf.json.JSONArray;



@Controller
@RequestMapping("/goods")
public class GoodsController {
	
	@Autowired
	public GoodsService goodsService;
	
	@RequestMapping("/getGoodsInfo")
	@ResponseBody
	public DayDetail getGoodsInfo(String shopname,String goodsname,String day){
		DayDetail  daydetail = goodsService.getGoodsInfo(shopname, goodsname, day);

		return daydetail;
		
	}
	
	/**
	 * 
	* @Title: index
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "/homepage")
	public String index(){
		return "user/pages/home";
	}
	
	/**
	 * 跳转统计分析页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getAnalysisView")
	public String getAnalysisView() {
		return "user/pages/analysis";
	}
	
	
	
	
	/**
	 * 获取店铺名称纬度
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getShopName" )
	@ResponseBody
	public String getShopName(HttpSession session) {
		List<Shop> shopList = goodsService.getShopName();
		
		/*for(int i=0;i<shopList.size();i++){
			String  name = shopList.get(i).getName();
			System.out.println("--"+shopList.get(i)+"---"+name);
		}*/
		JSONArray json = JSONArray.fromObject(shopList);
		return json.toString();
	}
	
	/**
	 * 获取商品名称纬度
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getGoodsName")
	@ResponseBody
	public String getGoodsName(HttpSession session) {
		List<Goods> goodstList = goodsService.getGoodsName();
		JSONArray json = JSONArray.fromObject(goodstList);
		return json.toString();
	}
	
	/**
	 * 根据时间商铺商品获取商品列表跳转统计分析
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getAnalysis",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getAnalysis(int PageNo, int pageSize, String starttime,
			String endtime,String shopcode,String goodscode) {

		HashMap<String,Object> map = goodsService.getGoodsByName( 
				shopcode,goodscode,starttime, endtime,PageNo, pageSize);
		List<Analysis> list=(List<Analysis>) map.get("analysis");
		

		EasyuiDatagridJson<Analysis> datagrid = new EasyuiDatagridJson<Analysis>();
		datagrid.setRows(list);
		long c = (long) map.get("pageCount");
		datagrid.setTotal(c);
		String str=JSON.toJSONString(datagrid);
		return str;
	}
	
	/**
	 * 新增数据页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getInsertdata")
	public String getInsertdata() {
		return "user/pages/insert";
	}
	
	/**
	 * 获取商品名称纬度
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getInsert")
	@ResponseBody
	public String getInsert(String shopcode,String goodscode,String starttime,String totalcount) {
		
		boolean isUpdate = goodsService.InsertGoods(starttime, shopcode, goodscode, totalcount);
		List<String> result = new ArrayList<String>();
		if (isUpdate) {
			result.add("修改成功！！");
		} else {
			result.add("修改失败！！");
		}
		JSONArray json = JSONArray.fromObject(result);
		return json.toString();
	}
	
	
	
}
